import { MySeriveProvider } from './../../../providers/my-serive/my-serive';
import { ComSeriveProvider } from './../../../providers/com-serive/com-serive';
import { LinergradientServiceProvider } from './../../../providers/linergradient-service/linergradient-service';
import { Component } from '@angular/core';
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController,
  App
} from 'ionic-angular';
import { BaseUI } from '../../../common/baseUI';

/**
 * Generated class for the ConfirmPasswordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-confirm-password',
  templateUrl: 'confirm-password.html'
})
export class ConfirmPasswordPage extends BaseUI {
  oldpassword: string = '';
  newpassword: string = '';
  repassword: string = '';

  isCheck: boolean = false;

  count = 0;
  

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public toastCtrl: ToastController,
    public lgService: LinergradientServiceProvider,
    public comSerive: ComSeriveProvider,
    public mySerive: MySeriveProvider,
    public app: App
  ) {
    super();
  }

  ionViewDidLoad() {
    
  }

  gotoUpDataInfo() {

    const stringuser = JSON.stringify({
      password: this.repassword
    });
    const user = { password: this.oldpassword };

    this.mySerive.checkOldPassword(user).subscribe(data => {
      if (data['status'] === 200) {
        if (
          this.newpassword.length >= 8 &&
          this.newpassword.length <= 16 &&
          this.repassword === this.newpassword
        ) {
          this.mySerive.updataImg(stringuser).subscribe(data => {
            this.isShowToast('密码修改成功');
            this.app.getRootNav().setRoot('LoginOrRegisterPage');
          });
        } else if (this.newpassword.length < 8 || this.repassword.length < 8) {
          this.isShowToast('密码长度不能少于8位');
        } else if (this.newpassword.length > 16 || this.newpassword.length > 16) {
          this.isShowToast('密码长度不能大于16位');
        } else if (this.repassword !== this.newpassword) {
          this.isShowToast('新密码与重复密码不相同');
        }
      } else if (data['status'] === 400) {
        this.isShowToast('请输入正确的旧密码');
      }
    });
  }

  // 限制重复弹窗
  isShowToast(message) {
    if (this.count === 0) {
      this.showToast(this.toastCtrl, message);
      this.count++;
      setTimeout(() => {
      this.count = 0;
      }, 3000);
    }
  }

  pop() {
    this.navCtrl.pop();
  }
}
